package com.ruoyi.test.api;

import com.ruoyi.test.povodto.R;
import com.ruoyi.test.povodto.User;
import com.ruoyi.test.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")  //使用在接口类上，对接口类的说明
public class UserController {
    @Autowired
    private UserService userService;
 
    @GetMapping("/list")
    @ApiOperation(value = "查询所有用户") // 接口方法的说明
    public R list(){
        List<User> list = userService.list();
        return  R.ok(list);
    }
 
    @GetMapping("/ById")
    @ApiOperation(value = "根据ID查询用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "用户id",required = true ,dataType = "int")
    )
    public  R ById(Integer id){
        User user = userService.getById(id);
        if (user != null){
            return  R.ok(user);
        }else {
            return  R.fail();
        }
    }
 
    /**
     * 插入学生信息
     * @param user
     */
    @PostMapping("/insert")
    @ApiOperation(value = "添加用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "user",value = "用户信息",required = true ,dataType = "User")
    )
    public R insertInfo(@RequestBody User user){
        boolean save = userService.save(user);
        if (save){
            return  R.ok(user);
        }
        return  R.fail();
    }
 
    /**
     * 根据id更新学生表信息
     * @param user
     */
    @PutMapping("/update")
    @ApiOperation(value = "根据ID修改用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "user",value = "用户信息",required = true ,dataType = "User")
    )
    public R updateById(@RequestBody User user){
        boolean save = userService.updateById(user);
        if (save){
            return  R.ok(user);
        }
        return  R.fail();
    }
 
    /**
     * 根据id删除学生信息
     * @param id
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据ID删除用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "用户id",required = true ,dataType = "int")
    )
    public R deleteById(String id){
        boolean b = userService.removeById(id);
        if (b){
            return  R.ok(id);
        }
        return  R.fail();
    }
 
}